class Message:
    def __init__(self, data,time):
        self.data = data
        self.time = time

    def get_data(self):
        return self.data

    def get_time(self):
        return self.time
    def get_message(self):
        return (self.data, self.time)
    def to_json(self):
        return {"data": self.data, "time": self.time}
    def __str__(self) -> str:
        print(self.to_json())
class Message_metadata:
    def __init__(self, create_time, update_time, data_length):
        self.create_time = create_time
        self.update_time = update_time
        self.data_length = data_length
    
    def get_create_time(self):
        return self.create_time

    def get_update_time(self):
        return self.update_time

    def get_data_length(self):
        return self.data_length
    def to_json(self):
        return {
            "create_time": self.create_time,
            "update_time": self.update_time,
            "data_length": self.data_length
        }
    def __str__(self) -> str:
        return(self.to_json())
class Message_queue:
    def __init__(self,metadata):
        self.metadata = metadata
        self.messages = []

    def add_message(self, message):
        self.metadata.update_time = message.get_time()
        self.metadata.data_length += 1
        print('in add',message.to_json())
        self.messages.append(message)

    def remove_message(self, index=0):
        # self.metadata.update_time = self.messages[-1].get_time()
        self.metadata.data_length -= 1
        if len(self.messages) == 0:
            raise IndexError("No messages in queue")
        if index >= len(self.messages):
            raise IndexError("Index out of range")
        del self.messages[index]

    def get_messages(self):
        return self.messages
    def get_message(self, index=0):
        return self.messages[index].to_json()
    def clear_messages(self):
        self.messages = []
    def to_json(self):
        json_list = []
        if self.metadata is not None:
            json_list.append({"metadata":self.metadata.to_json()})
        for msg in self.messages:
            json_list.append({"data":msg.get_data(), "time":msg.get_time()})
        
        return json_list
    def pure_to_json(self):
        json_list = []
        for msg in self.messages:
            json_list.append({"data":msg.get_data()})
        print(json_list)
        return json_list
    def __str__(self) -> str:
        print(self.to_json())